package com.mes.biz.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import com.github.pagehelper.PageHelper;
import com.mes.biz.domain.*;
import com.mes.biz.mapper.BizOrderMapper;
import com.mes.biz.service.BizOrderService;
import com.mes.common.core.domain.entity.SysDept;
import com.mes.common.core.domain.entity.SysUser;
import com.mes.common.core.page.PageDomain;
import com.mes.common.exception.BizException;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.sql.SqlUtil;
import com.mes.framework.cache.DeptUtils;
import com.mes.framework.cache.UserUtils;
import com.mes.system.domain.SysTenant;
import com.mes.system.service.SysTenantService;
import com.mes.system.strategy.AutoCodeUtil;
import org.apache.commons.compress.utils.Sets;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.biz.mapper.BizMeasureMapper;
import com.mes.biz.service.BizMeasureService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 测量单Service业务层处理
 * @author swf
 * @email swf
 * @date 2023-09-04
 */
@Service
@Transactional(readOnly = true)
public class BizMeasureServiceImpl extends BaseServiceImpl<BizMeasureMapper, BizMeasure> implements BizMeasureService {

    private static final Logger log = LoggerFactory.getLogger(BizMeasureServiceImpl.class);

    @Resource
    private BizMeasureMapper bizMeasureMapper;
    @Resource
    private AutoCodeUtil autoCodeUtil;
    @Resource
    private SysTenantService sysTenantService;
    @Resource
    private BizOrderService bizOrderService;

    /**
     * 获取单条数据
     * @param bizMeasure 测量单
     * @return 测量单
     */
    @Override
    public BizMeasure get(BizMeasure bizMeasure) {
        BizMeasure dto = super.get(bizMeasure);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 测量单id
     * @return 测量单
     */
    @Override
    public BizMeasure get(String id) {
        BizMeasure dto = super.get(id);
        return dto;
    }

    /**
     * 查询测量单列表
     * @param bizMeasure 测量单
     * @return 测量单
     */
    @Override
    public List<BizMeasure> findList(BizMeasure bizMeasure) {
		List<BizMeasure> bizMeasureList = super.findList(bizMeasure);
        if (!CollectionUtils.isEmpty(bizMeasureList)) {
            bizMeasureList.stream().forEach(dto -> {
                SysUser createUser = UserUtils.getSysUser(dto.getCreateBy());
                if (StringUtils.isNotEmpty(createUser.getDeptId())){
                    SysDept sysDept = DeptUtils.getSysDept(createUser.getDeptId());
                    if (null != sysDept){
                        dto.setDeptIdName(sysDept.getDeptName());
                    }
                }
            });
        }
        return bizMeasureList;
    }

    /**
     * 分页查询测量单列表
     * @param bizMeasure 测量单
     * @return 测量单
     */
    @Override
    public PageInfo<BizMeasure> findPage(BizMeasure bizMeasure) {
		PageInfo<BizMeasure> page = super.findPage(bizMeasure);
        return page;
    }

    /**
     * 保存测量单
     * @param bizMeasure
     * @return 结果
     */
    @Override
    public boolean save(BizMeasure bizMeasure) {
        return super.save(bizMeasure);
    }

    /**
     * 删除测量单信息
     * @param bizMeasure
     * @return 结果
     */
    @Override
    public boolean remove(BizMeasure bizMeasure) {
        return super.remove(bizMeasure);
    }

    /**
     * 批量删除测量单
     * @param ids 需要删除的测量单ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizMeasureByIds(String[] ids) {
        return mapper.deleteBizMeasureByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    /**
     * 创建测量单
     * @param bizMeasure
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public boolean create(BizMeasure bizMeasure) {
        //return false;
        String tenantId = SecurityUtils.getTenantId();
        bizMeasure.setTenantId(tenantId);
        if(StringUtils.isEmpty(bizMeasure.getMeasureNumber())) {
            SysTenant sysTenant = sysTenantService.get(tenantId);
            if (StringUtils.isEmpty(sysTenant.getLeader())){
                throw new BizException("请联系管理员添加测量单前缀!");
            }
            String code = autoCodeUtil.genSerialCode("MEASURE_CODE",sysTenant.getLeader());
            bizMeasure.setMeasureNumber(code);
        } else {
            if(getBizMeasureByMeasureNumber(bizMeasure.getMeasureNumber())!=null) {
                throw new BizException("测量单编号已存在!");
            }
        }
        return save(bizMeasure);
    }

    /**
     * 修改测量单
     * @param bizMeasure
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public boolean update(BizMeasure bizMeasure) {
        //return false;
        if(StringUtils.isEmpty(bizMeasure.getMeasureNumber())) {
            throw new BizException("测量单编号不能为空!");
        }
        BizMeasure existsMeasureNumber= getBizMeasureByMeasureNumber(bizMeasure.getMeasureNumber());
        if(existsMeasureNumber != null && !existsMeasureNumber.getId().equals(bizMeasure.getId()) ) {
            throw new BizException("测量单编号已存在!");
        }
        return save(bizMeasure);
    }

    /**
     * 根据测量单编号查询测量单信息
     * @param measureNumber
     * @return
     */
    @Override
    public BizMeasure getBizMeasureByMeasureNumber(String measureNumber) {
        return bizMeasureMapper.getBizMeasureByMeasureNumber(measureNumber);
    }

    @Override
    public PageInfo<BizMeasure> appletList(BizMeasure bizMeasure) {
        PageDomain page = bizMeasure.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(bizMeasureMapper.appletList(bizMeasure));
    }

}
